{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a20a2420",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "多项式p1: Poly(2*x**3 - 2*x**2 + x + 1, x, modulus=5)\n",
      "多项式p2: Poly(x**2 - x - 1, x, modulus=5)\n",
      "加法结果: Poly(2*x**3 - x**2, x, modulus=5)\n",
      "减法结果: Poly(2*x**3 + 2*x**2 + 2*x + 2, x, modulus=5)\n",
      "乘法结果: Poly(2*x**5 + x**4 + x**3 + 2*x**2 - 2*x - 1, x, modulus=5)\n",
      "取余运算: Poly(-2*x + 1, x, modulus=5)\n",
      "商: Poly(2*x, x, modulus=5)\n",
      "最大公约数: Poly(x + 2, x, modulus=5)\n",
      "在多项式 p1 中代入 x = 2 的结果: 1\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, Poly, GF, gcd\n",
    "\n",
    "# 定义符号变量\n",
    "x = symbols('x')\n",
    "\n",
    "# 定义多项式，系数在整数模5环中\n",
    "p1 = Poly(2*x**3 + 3*x**2 + x + 1, x, domain=GF(5))\n",
    "p2 = Poly(x**2 + 4*x + 4, x, domain=GF(5))\n",
    "\n",
    "# 打印多项式\n",
    "print(\"多项式p1:\", p1)\n",
    "print(\"多项式p2:\", p2)\n",
    "\n",
    "\n",
    "# 多项式加法\n",
    "add_result = p1 + p2\n",
    "print(\"加法结果:\", add_result)\n",
    "\n",
    "# 多项式减法\n",
    "sub_result = p1 - p2\n",
    "print(\"减法结果:\", sub_result)\n",
    "\n",
    "# 多项式乘法\n",
    "mul_result = p1 * p2\n",
    "print(\"乘法结果:\", mul_result)\n",
    "\n",
    "# 注意：在整数模n环中，不能总是进行多项式除法\n",
    "# 但可以进行模运算\n",
    "mod_result = p1 % p2\n",
    "print(\"取余运算:\", mod_result)\n",
    "\n",
    "# 求商\n",
    "quotient_result = p1 // p2\n",
    "print(\"商:\", quotient_result)\n",
    "\n",
    "# 求最大公约数\n",
    "gcd_result = gcd(p1, p2)\n",
    "print(\"最大公约数:\", gcd_result)\n",
    "\n",
    "# 代入值求解\n",
    "value_result = p1.subs(x, 2)\n",
    "print(\"在多项式 p1 中代入 x = 2 的结果:\", value_result)\n",
    "\n",
    "## 输出示例\n",
    "# 多项式p1: Poly(2*x**3 - 2*x**2 + x + 1, x, modulus=5)\n",
    "# 多项式p2: Poly(x**2 - x - 1, x, modulus=5)\n",
    "# 加法结果: Poly(2*x**3 - x**2, x, modulus=5)\n",
    "# 减法结果: Poly(2*x**3 + 2*x**2 + 2*x + 2, x, modulus=5)\n",
    "# 乘法结果: Poly(2*x**5 + x**4 + x**3 + 2*x**2 - 2*x - 1, x, modulus=5)\n",
    "# 取余运算: Poly(-2*x + 1, x, modulus=5)\n",
    "# 商: Poly(2*x, x, modulus=5)\n",
    "# 最大公约数: Poly(x + 2, x, modulus=5)\n",
    "# 在多项式 p1 中代入 x = 2 的结果: 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f1b05a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
